package com.example.fireassistant_server.service.impl;

import com.example.fireassistant_server.dto.user.UserCreateRequest;
import com.example.fireassistant_server.dto.user.UserDto;
import com.example.fireassistant_server.dto.wechat.WechatUserCreateRequest;
import com.example.fireassistant_server.dto.wechat.WechatUserDto;
import com.example.fireassistant_server.dto.wechat.WechatUserSearchFilter;
import com.example.fireassistant_server.entity.User;
import com.example.fireassistant_server.entity.WeChatUser;
import com.example.fireassistant_server.exception.BizException;
import com.example.fireassistant_server.exception.ExceptionType;
import com.example.fireassistant_server.mapper.UserMapper;
import com.example.fireassistant_server.mapper.WechatUserMapper;
import com.example.fireassistant_server.repository.UserRepository;
import com.example.fireassistant_server.repository.WechatUserRepository;
import com.example.fireassistant_server.repository.spec.WechatUserSpecification;
import com.example.fireassistant_server.service.UserService;
import com.example.fireassistant_server.service.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class WechatUserServiceImpl implements WechatUserService {

    private WechatUserMapper wechatUserMapper;

    private WechatUserRepository wechatUserRepository;

    private UserMapper userMapper;
    private UserService userService;
    @Override
    public WechatUserDto createWechatUser(String openID, UserCreateRequest userCreateRequest) {
        UserDto userDto = userService.create(userCreateRequest);
        WeChatUser weChatUser = new WeChatUser();
        weChatUser.setOpenId(openID);
        weChatUser.setUser(userMapper.createUser(userDto));
        WeChatUser weUser = wechatUserRepository.save(weChatUser);
        return getCurrentUser(weUser.getOpenId());
    }
    @Override
    public WechatUserDto getCurrentUser(String openId) {
        Optional<WeChatUser> weChatUser = wechatUserRepository.getWeChatUserByOpenId(openId);
        if (!weChatUser.isPresent()) {
            throw new BizException(ExceptionType.WECHAT_USER_NOT_FOUND);
        }
        return wechatUserMapper.toDto(weChatUser.get());
    }

    @Override
    public WechatUserDto getWechatUserByUser(User user) {
        Optional<WeChatUser> weChatUser = wechatUserRepository.getWeChatUserByUser(user);



        if (!weChatUser.isPresent()) {
            throw new BizException(ExceptionType.WECHAT_USER_NOT_FOUND);
        }
        return wechatUserMapper.toDto(weChatUser.get());
    }

    @Override
    public void deleteWechatUserByUser(User user) {
        Optional<WeChatUser> weChatUser = wechatUserRepository.getWeChatUserByUser(user);
        if (!weChatUser.isPresent()) {
            throw new BizException(ExceptionType.WECHAT_USER_NOT_FOUND);
        }
        wechatUserRepository.deleteByUserId(user.getId());
    }


    //条件查询
    @Override
    public Page<WechatUserDto> search(WechatUserSearchFilter filter) {
        WechatUserSpecification specs = new WechatUserSpecification();
        return wechatUserRepository.findAll(specs, filter.toPageable()).map(wechatUserMapper::toDto);
    }


    @Autowired
    public void setWechatUserMapper(WechatUserMapper wechatUserMapper) {
        this.wechatUserMapper = wechatUserMapper;
    }

    @Autowired
    public void setWechatUserRepository(WechatUserRepository wechatUserRepository) {
        this.wechatUserRepository = wechatUserRepository;
    }


    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
}
